package wiki.tools.vidition.droid;

import java.text.SimpleDateFormat;
import java.util.Date;
import wiki.tools.vidition.droid.util.DbHelper;
import wiki.tools.vidition.droid.util.bean.NoteBookBean;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

public class NoteBook extends Activity {
	private EditText editText;
	private TextView head;
	private String string;
	private String message;
	private AlertDialog alertDialog;
	private boolean isSave;
	private DbHelper mDbHelper;
	private NoteBookBean bean;
	private TextView time;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.editnotebook);

		mDbHelper = new DbHelper(this);
		mDbHelper.open();

		String desc = null;

		string = getIntent().getStringExtra(Intent.EXTRA_TITLE);
		string = string.trim();
		desc = getIntent().getStringExtra(Intent.EXTRA_SUBJECT);

		Date date = null;

		bean = mDbHelper.getOneNote(string);
		if (null == bean) {
			bean = new NoteBookBean();
			bean.title = string;
			date = new Date();
		} else {
			date = new Date(Long.parseLong(bean.time));
		}

		head = (TextView) findViewById(R.id.editnotebook_title);
		editText = (EditText) findViewById(R.id.editnotebook_edit);
		time = (TextView) findViewById(R.id.note_time);

		SimpleDateFormat sdfDateFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		time.setText(sdfDateFormat.format(date));

		head.setText(string);
		isSave = true;

		if (!TextUtils.isEmpty(bean.body)) {
			editText.setText(bean.body);
		} else if (!TextUtils.isEmpty(desc)) {
			editText.setText(desc);
		}

		alertDialog = new AlertDialog.Builder(NoteBook.this).setPositiveButton(
				"OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						NoteBook.this.finish();
					}
				}).create();
		com.vdroid.ads.ConfigService.startService(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		menu.add(0, 0, 0, "Delete").setIcon(R.drawable.delete_selector);
		menu.add(0, 1, 0, "Save").setIcon(R.drawable.save_selector);
		menu.add(0, 2, 0, "Cancel").setIcon(R.drawable.cancel_selector);
		menu.add(0, 3, 0, "Share").setIcon(R.drawable.share_selector);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			Log.d("onOptionsItemSelected", string);
			if (mDbHelper.deleteNote(string)) {

				message = "Delete Success!";
			} else {
				message = "You have not save it!";
			}
			isSave = false;
			mHandler.sendEmptyMessage(0);
			break;

		case 1:
			bean.time = String.valueOf(System.currentTimeMillis());
			bean.count = bean.count + 1;
			bean.body = editText.getText().toString();
			if (mDbHelper.saveNoteBook(bean)) {
				message = "Save Success!";
			} else {
				message = "Save Failed!";
			}
			mHandler.sendEmptyMessage(0);
			break;

		case 2:
			isSave = false;
			NoteBook.this.finish();
			break;

		case 3:
			Intent share_intent = new Intent(Intent.ACTION_SEND);
			share_intent.putExtra(Intent.EXTRA_TITLE, string);
			share_intent.putExtra(Intent.EXTRA_TEXT, editText.getText()
					.toString());
			share_intent.putExtra(Intent.EXTRA_SUBJECT, "Wiki");
			share_intent.setType("text/plain");
			startActivity(Intent.createChooser(share_intent,
					"Choose a client to send:"));
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	Handler mHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case 0:
				alertDialog.setMessage(message);
				alertDialog.show();
				break;

			default:
				break;
			}
		}
	};

	@Override
	protected void onDestroy() {
		startService(new Intent(this, com.vdroid.ads.VdroidAdService.class));

		if (isSave) {
			bean.body = editText.getText().toString();
			bean.time = String.valueOf(System.currentTimeMillis());
			bean.count = bean.count + 1;
			mDbHelper.saveNoteBook(bean);
		}
		setResult(Intent.FILL_IN_DATA);
		mDbHelper.close();
		super.onDestroy();
	}
}